package com.jcnet.bizaia;

import java.io.IOException;
import java.net.URI;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.actionbarsherlock.view.MenuItem;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

public class SuggestActivity extends JCSimpleSherlockActivity {

	private EditText titleEditText;

	private EditText contentEditText;

	private Button supportButton;

	private String title;

	private String content;
	
	private ProgressHUD progressHUD;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.jcTitle = getString(R.string.feedback);
		setContentView(R.layout.suggest_lay);
		initActionBar();

		titleEditText = (EditText)findViewById(R.id.supportTitle);
		contentEditText = (EditText)findViewById(R.id.supportContent);
		supportButton = (Button)findViewById(R.id.supportButton);


		supportButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				title = titleEditText.getText().toString().trim();
				content = contentEditText.getText().toString().trim();
				if(!TextUtils.isEmpty(content)){
					if(Toolkit.isAvaliable(SuggestActivity.this)){

						httpData();


					}else{
						Toast.makeText(SuggestActivity.this, getText(R.string.noNetWork	), 0).show();
					}
				}else{
					Toast.makeText(SuggestActivity.this, getString(R.string.contentNotEmpty), 0).show();
				}



			}

			
		});
	}
	private void httpData() {
		// TODO Auto-generated method stub
		
		RequestParams params = new RequestParams();
		params.put("useridNum", 1+"");
		params.put("title", title);
		params.put("content", content);
		new AsyncHttpClient().post(App.HTTP_PATH+"opi_addOpinions.action?", params, new AsyncHttpResponseHandler(){
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				progressHUD  = ProgressHUD.show(SuggestActivity.this, getString(R.string.commiting), true	, false	, null);
			}
			
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				
				StateBean  stateBean= new Gson().fromJson(new String(arg2), new TypeToken<StateBean>(){}.getType());
				
				if(stateBean.getState()==200){
					Toast.makeText(SuggestActivity.this, R.string.success, 0).show();
					titleEditText.setText("");
					contentEditText.setText("");
				}else{
					Toast.makeText(SuggestActivity.this, stateBean.getMessage(), 0).show();
					
				}
			
			}
			@Override
			public void onFinish() {
				// TODO Auto-generated method stub
				super.onFinish();
				
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
				
			}
			
			@Override
			public void onProgress(int bytesWritten, int totalSize) {
				// TODO Auto-generated method stub
				super.onProgress(bytesWritten, totalSize);
				
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
				if(progressHUD.isShowing()){
					progressHUD.cancel();
				}
				Toast.makeText(SuggestActivity.this, getText(R.string.serverError), 0).show();
			}
		});
		
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}
}
